package com.jgsu.farm.controller;

import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.*;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/question", tags = "咨询信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;

    /**
     * 咨询添加
     */
    @ApiOperation(value = "咨询添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult questionAdd(Question question) {
        try {
            questionService.add(question);
            log.info("咨询添加成功，植保知识title为：{}", question.getTitle());
            return CommonResult.success(question);
        } catch (DataAddException e) {
            log.error("咨询添加异常,title为：{}", question.getTitle());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 咨询信息删除
     */
    @ApiOperation(value = "咨询信息删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult questionDelete(@RequestParam("questid") Integer questid) {
        try {
            questionService.delete(questid);
            log.info("删除id为：的咨询信息",questid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("不存在id为：{}的咨询信息，删除失败",questid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 咨询信息查询功能
     */
    @ApiOperation(value = "咨询信息查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Question>> questionList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                         @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Question> questions = questionService.selectAll(page, pagesize);
            log.info("查询所有的咨询信息");
            return CommonResult.success(questions);
        } catch (DataNotExistException e) {
            log.error("咨询信息表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 咨询信息修改
     */
    @ApiOperation(value = "咨询信息修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult questionUpdate(Question question) {
        try {
            questionService.update(question);
            log.info("更新title为{}的咨询信息成功", question.getTitle());
            return CommonResult.success(question);
        } catch (DataNotExistException e) {
            log.error("更新title为{}的咨询信息失败", question.getTitle());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
